const puppeteer = require('puppeteer');
const fs = require('fs');
const request = require('request');
const path = require('path');

// 单个作品爬取

const url = 'https://www.zcool.com.cn/work/ZMjc1ODMzMDA=.html'   //目标地址列表
const file = 'zcool/product'                                     //目标保存文件夹
const fileName = 'h2'                                 //爬取页面标题作为次此文件名字
const regular = '.photo-information-content > img'              //爬取规律
const closPupLogin ='//*[@id="popLogin"]/div[1]'           //关闭弹窗
const autoRolling = true ;                     //是否开启懒加载 false：关闭  true：开启

// 爬取 站酷图片数据
async function getPhoto() {
    let browser = await puppeteer.launch({
        headless: false,        //开启视图
        defaultViewport: null,
    });
    let page = await browser.newPage();
    console.log('······打开页面······')
    await page.goto(url);
    await page.waitFor(1000)
    console.log('······检测登录弹窗······')
    popLogin = await page.$x(closPupLogin)
    if (popLogin.length > 0) {
        await popLogin[0].click()           //关闭登录弹窗
        console.log('······关闭登录弹窗······')
    }
    if(autoRolling){
        console.log('······开始滚动窗口······')
        await autoScroll(page);
        console.log('······滚动到底-结束······')
        }
    let title = await page.$(fileName);
    let name = await (await title.getProperty('innerText')).jsonValue(); // 获取名称
    console.log('······📃 '+name+'开始保存······')
    let images = await page.$$eval(regular, el => el.map(x => x.getAttribute('src')));
    mkdirSync(`./${file}/${name}`);        // 存放目录
    for (i of images) {
        var h = i.lastIndexOf(".");     //取最后 . 的位置
        var type = i.substr(h + 0);     //获取包括 . 之后的后缀
        await downloadImg(i, './' + file + '/' + name + '/' + new Date().getTime() + type);
    }
    console.log('······✅ '+name+' 保存完成-关闭页面-结束任务 ······')
    // 关闭任务
    await browser.close();
};

// 同步创建目录
function mkdirSync(dirname) {
    if (fs.existsSync(dirname)) {
        return true;
    } else {
        if (mkdirSync(path.dirname(dirname))) {
            fs.mkdirSync(dirname);
            return true;
        }
    }
    return false
}

// 下载文件 保存图片
async function downloadImg(src, path) {
    return new Promise(async function (resolve, reject) {
        let writeStream = fs.createWriteStream(path);
        let readStream = await request(src);
        await readStream.pipe(writeStream);
        readStream.on('end', function () {
          console.log('······图片下载成功👌 ······');
        });
        readStream.on('error', function () {
            console.log("······错误信息❌:" + err + '······')
        })
        writeStream.on("finish", function () {
            console.log("······图片保存成功······");
            writeStream.end();
            resolve();
        });
    })
}

// 滑动屏幕，滚至页面底部，懒加载
function autoScroll(page) {
    return page.evaluate(() => {
        return new Promise((resolve) => {
            var totalHeight = 0;
            var distance = 500;
            // 每500毫秒让页面下滑500像素的距离
            var timer = setInterval(() => {
                var scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;
                if (totalHeight >= scrollHeight) {
                    clearInterval(timer);
                    resolve();
                }
            }, 500);
        })
    });
}

getPhoto()
